import request from '@/common/request/Api.js'

let datas = []
request.judgeQuestionAPI().then(res => {
	datas = res.data
})


// NL 能力维度
function getPingNL() {
	let list = datas.NL
	return list;
}

// XG 性格维度（MBTI测试）
function getPingXG() {
	let list = datas.XG
	return list;
}


// XG 性格维度（MBTI测试）
function getPingXK() {
	let list = datas.XK
	return list;
}


// XG 性格维度（MBTI测试）
function getPingXQ() {
	let list = datas.XQ
	return list;
}

// XG 性格维度（MBTI测试）
function getPingZY() {
	let list = datas.ZY
	return list;
}


function getPingQuest(israndom = true) {
	let quest = [...getPingXQ(), ...getPingXG(), ...getPingNL(), ...getPingZY(), ...getPingXK()]
	if (israndom) {
		quest = randomArray(quest)
	}
	return quest;
}

function randomArray(array) {
	let _skey = [];
	array.forEach((_, index) => {
		_skey.push({
			"i": index,
			'r': Math.random()
		});
	});
	_skey = _skey.sort((item1, item2) => item1.r > item2.r);
	let _array = [];
	_skey.forEach(item => {
		_array.push(array[item.i])
	});
	return _array;
}

function S4() {
	return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

function guid() {
	return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

function dateTimeStr(str = 'y-m-d h:i:s') {
	let date = new Date(),
		year = date.getFullYear(), //年
		month = date.getMonth() + 1, //月
		day = date.getDate(), //日
		hour = date.getHours(),
		minute = date.getMinutes(),
		second = date.getSeconds();
	month >= 1 && month <= 9 ? (month = "0" + month) : "";
	day >= 0 && day <= 9 ? (day = "0" + day) : "";
	hour >= 0 && hour <= 9 ? (hour = "0" + hour) : "";
	minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
	second >= 0 && second <= 9 ? (second = "0" + second) : "";
	if (str.indexOf('y') != -1) {
		str = str.replace('y', year)
	}
	if (str.indexOf('m') != -1) {
		str = str.replace('m', month)
	}
	if (str.indexOf('d') != -1) {
		str = str.replace('d', day)
	}
	if (str.indexOf('h') != -1) {
		str = str.replace('h', hour)
	}
	if (str.indexOf('i') != -1) {
		str = str.replace('i', minute)
	}
	if (str.indexOf('s') != -1) {
		str = str.replace('s', second)
	}
	return str;
}

function formatTimeStr(seconds) {
	const seconds1 = parseInt(seconds);
	const minutes = parseInt(seconds1 / 60);
	const _seconds = seconds1 % 60;
	let str = _seconds + "秒";
	if (minutes > 0) {
		if (_seconds < 10) {
			str = "0" + str
		}
		str = minutes + "分" + str
	}
	return str;
}

export default {
	getPingQuest: getPingQuest,
	guid: guid,
	dateTimeStr: dateTimeStr,
	formatTimeStr: formatTimeStr,
}